package com.seed.application.service.detail;

import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.seed.core.action.DetailAction;
import com.seed.core.builder.datapermis.DataPermisBuilder;
import com.seed.core.builder.datapermis.DataPermisWrapper;
import com.seed.core.builder.declare.DeclaredBuilder;
import com.seed.core.builder.declare.DeclaredWrapper;
import com.seed.core.builder.query.QueryBuilder;
import com.seed.core.field.FieldContext;
import com.seed.core.listener.OnSetListener;
import com.seed.core.pojo.Const;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Declared;
import com.seed.core.query.pojo.Searched;
import com.seed.util.SeedKit;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class DetailService {
    private final Seed seed; // 当前模块
    private final DetailAction action;
    private List<List<String>> dataPermis; // 数据权限
    private Map<String, String> fieldPermits; // 字段权限

    public DetailService(Seed seed, DetailAction action) {
        this.seed = seed;
        this.action = action;
    }

    public Row exec(Serializable id) {
        QueryBuilder builder = new QueryBuilder(seed);

        Map<String, Declared> declareds = new DeclaredBuilder(seed, getDeclares()).build();
        if(!declareds.isEmpty()) builder.declaredWrapper(new DeclaredWrapper(seed, declareds));

        List<Map<String, Searched>> dataPermised = new DataPermisBuilder(seed, dataPermis).build();
        if(!dataPermised.isEmpty()) builder.dataPermisWrapper(new DataPermisWrapper(seed, dataPermised));

        QueryTable queryTable = new QueryTable(seed.getTableName()).as("t");
        QueryWrapper qw = builder.build().where(new QueryColumn(queryTable, Const.ID).eq(id));
        Row row = Db.selectOneByQuery(qw);
        if(row == null) throw new RuntimeException("数据不存在或没有权限");

        builder.fills(List.of(row));
        onSet(row);
        fieldPermit(row);

        return row;
    }

    private List<String> getDeclares() {
        // 带路径的配置
        List<String> declares = action.getDeclares();
        if(declares != null) return declares;

        declares = seed.getDeclares();
        if(declares != null) return declares;

        return SeedKit.getDefaultDeclares(seed);
    }

    private void onSet(Row self) {
        List<? extends OnSetListener> onSetListeners = SeedKit.getOnSet(seed);
        if(onSetListeners == null || onSetListeners.isEmpty()) return;

        for(OnSetListener onSet: onSetListeners) onSet.onSet(self);
    }

    private void fieldPermit(Row self) {
        if(fieldPermits == null) return;

        for(Map.Entry<String, String> entry: fieldPermits.entrySet()) {
            String finalColumn = entry.getKey();
            String value = entry.getValue();
            FieldContext.auth(value, self, finalColumn);
        }
    }

    public DetailService dataPermis(List<List<String>> dataPermis) {
        this.dataPermis = dataPermis;
        return this;
    }

    public DetailService fieldPermits(Map<String, String> fieldPermits) {
        this.fieldPermits = fieldPermits;
        return this;
    }
}
